﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;
using YunNanHealthProject_Register.Entity;
using YunNanHealthProject_Register.Model;

namespace YunNanHealthProject_Register.service
{
    class UpLoadInfoService
    {
        private BasicInfo basicInfo = new BasicInfo();
        private UpResourceInfo upResourceInfo = new UpResourceInfo();
        string pathJson = Application.StartupPath + "\\MZSchedule_Wonder.json";

        //webservice实例化
        WebReference.AppointmentServer appointmentServer = new WebReference.AppointmentServer();


        //获取当前日期是星期几
        private int[] Day = new int[] { 7, 1, 2, 3, 4, 5, 6 };

        //一级科室信息
        public string UpLevel1KS()
        {
            List<KSCodeLevel1> list = basicInfo.GetKSCode();
            XmlDocument xmldoc = new XmlDocument();
            XmlElement xmlRoot = XmlHeader(xmldoc);
            //列表
            XmlElement List = xmldoc.CreateElement("List");
            foreach (KSCodeLevel1 kscode in list)
            {
                XmlData(kscode, "DeptInfo", xmldoc, List);
            }
            xmlRoot.AppendChild(List);
            return GetResult(appointmentServer.UpDeptInfoTopService(xmldoc.InnerXml));
        }

        //二级科室信息
        public string UpLevel2KS()
        {
            List<KSCodeLevel2> list = basicInfo.GetKSCode2();
            XmlDocument xmldoc = new XmlDocument();
            XmlElement xmlRoot = XmlHeader(xmldoc);
            //列表
            XmlElement List = xmldoc.CreateElement("List");
            foreach (KSCodeLevel2 kscode in list)
            {
                XmlData(kscode, "DeptInfo", xmldoc, List);
            }
            xmlRoot.AppendChild(List);
            return GetResult(appointmentServer.UpDeptInfoTwoService(xmldoc.InnerXml));
        }

        //上传医生基本信息
        public string UpYSInfo()
        {
            List<YSCode> list = basicInfo.GetYSCodes();
            XmlDocument xmldoc = new XmlDocument();
            XmlElement xmlRoot = XmlHeader(xmldoc);
            //列表
            XmlElement List = xmldoc.CreateElement("List");
            foreach (YSCode yscode in list)
            {
                XmlData(yscode, "ResourceInfo", xmldoc, List);
            }
            xmlRoot.AppendChild(List);
            //Console.WriteLine(xmldoc.InnerXml);
            return GetResult(appointmentServer.UpDoctInfoService(xmldoc.InnerXml));
        }

        //门诊信息注册
        public string UpMZInfo()
        {
            List<OutPatientKSInfo> list = basicInfo.GetOutPatientKSInfos();
            XmlDocument xmldoc = new XmlDocument();
            XmlElement xmlRoot = XmlHeader(xmldoc);
            //列表
            XmlElement List = xmldoc.CreateElement("List");
            foreach (OutPatientKSInfo outPatientInfo in list)
            {
                XmlData(outPatientInfo, "ResourceInfo", xmldoc, List);
            }
            xmlRoot.AppendChild(List);
            return GetResult(appointmentServer.UpOutPatInfoService(xmldoc.InnerXml));
        }

        //停诊信息上传(这个要实时调) TODO 加入日志记录
        public string UpStopScheduleInfo()
        {
            List<UpStopInfo> list = upResourceInfo.GetUpStopInfos(Day[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString());
            XmlDocument xmldoc = new XmlDocument();
            XmlElement xmlRoot = XmlHeader(xmldoc);
            //列表
            XmlElement List = xmldoc.CreateElement("List");
            try
            {
                foreach (UpStopInfo num in list)
                {
                    XmlData(num, "NumSourceInfo", xmldoc, List);
                }
                xmlRoot.AppendChild(List);
                WebReference.AppointmentServer appointmentServer = new WebReference.AppointmentServer();
                return GetResult(appointmentServer.UpStopScheduleInfoService(xmldoc.InnerXml));
            }
            catch (NullReferenceException)
            {
                return "无停诊信息";
            }

        }

        //预约就诊情况上传（需要一起联调）
        public string UpOrderMedicin()
        {
            List<UpOrderMedicin> list = upResourceInfo.GetUpOrderMedicin("2017-07-01", "2017-07-05");
            XmlDocument xmldoc = new XmlDocument();
            XmlElement xmlRoot = XmlHeader(xmldoc);
            //列表
            XmlElement List = xmldoc.CreateElement("List");
            try
            {
                foreach (UpOrderMedicin num in list)
                {
                    XmlData(num, "OrderInfo", xmldoc, List);
                }
                xmlRoot.AppendChild(List);
                return GetResult(appointmentServer.UpOrderMedicinService(xmldoc.InnerXml));
            }
            catch (NullReferenceException)
            {
                return "查询失败,请联系管理员";
            }
        }

        //根据实体生成数据
        public XmlElement XmlData(object t, string outStr, XmlDocument xmldoc, XmlElement xmlList)
        {
            XmlElement outElement = xmldoc.CreateElement(outStr);

            foreach (System.Reflection.PropertyInfo property in t.GetType().GetProperties())
            {
                Object[] objs = property.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objs.Length == 0)
                {
                    XmlElement xmlElement = xmldoc.CreateElement(property.Name);
                    try
                    {
                        XmlText xmlText = xmldoc.CreateTextNode(property.GetValue(t).ToString());
                        xmlElement.AppendChild(xmlText);
                    }
                    catch (NullReferenceException)
                    {
                        XmlText xmlText = xmldoc.CreateTextNode("");
                        xmlElement.AppendChild(xmlText);
                    }
                    outElement.AppendChild(xmlElement);
                }
            }
            xmlList.AppendChild(outElement);
            return xmlList;
        }

        //头信息
        public XmlElement XmlHeader(XmlDocument xmldoc)
        {
            XmlDeclaration xmldecl = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmldoc.AppendChild(xmldecl);
            //加入一个根元素
            XmlElement xmlRoot = xmldoc.CreateElement("Request");
            xmldoc.AppendChild(xmlRoot);
            //信息头
            XmlElement messageHeader = xmldoc.CreateElement("MessageHeader");
            //账号信息
            XmlElement accessAccount = xmldoc.CreateElement("accessAccount");
            XmlText accessAccountText = xmldoc.CreateTextNode("43120477700");
            accessAccount.AppendChild(accessAccountText);
            //签名
            XmlElement sign = xmldoc.CreateElement("sign");
            XmlText signText = xmldoc.CreateTextNode("43120477700");
            sign.AppendChild(signText);
            //完成头部信息
            messageHeader.AppendChild(accessAccount);
            messageHeader.AppendChild(sign);
            xmlRoot.AppendChild(messageHeader);
            return xmlRoot;
        }

        //解析返回值
        public string GetResult(string xmlStr)
        {
            var doc = new XmlDocument();
            doc.LoadXml(xmlStr);

            var rowNoteList = doc.SelectNodes("/Response/MessageHeader");


            if (rowNoteList != null)
            {
                foreach (XmlNode rowNode in rowNoteList)
                {
                    var fieldNodeList = rowNode.ChildNodes;
                    foreach (XmlNode courseNode in fieldNodeList)
                    {
                        if (courseNode.Name.Equals("desc"))
                        {
                            return courseNode.InnerText;
                        }
                    }
                }
            }
            return "无返回值";
        }
    }
}
